//
//  HJMessageFileManager.m
//  KaiChat
//
//  Created by lx on 2023/11/22.
//

#import "HJMessageFileManager.h"
#import "HJChatMessageModel.h"
#import "HJChatSessionModel.h"


@interface HJMessageFileManager ()

@property (nonatomic, copy, nullable) NSString *rootCacheFinder;

@end


@implementation HJMessageFileManager

static HJMessageFileManager *_fileManager = nil;


+ (HJMessageFileManager *)shareManager {
    if (_fileManager) return _fileManager;
    return [[HJMessageFileManager alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_fileManager == nil) {
            _fileManager = [super allocWithZone:zone];
        }
    });
    return _fileManager;
}


+ (NSString *)pathWithType:(NSString *)type sessionId:(NSString *)sessionId fileName:(NSString *)fileName {
    NSString *finderPath = [[HJMessageFileManager.shareManager rootCacheFinder] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@/%@", HJUserInfo.userId, sessionId, type]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:finderPath]) {
        BOOL res = [[NSFileManager defaultManager] createDirectoryAtPath:finderPath withIntermediateDirectories:YES attributes:nil error:nil];
        if (!res) {
            return nil;
        }
    }
    return [finderPath stringByAppendingPathComponent:fileName];
}

+ (NSString *)imagefileFilePathWithSessionId:(NSString *)sessionId fileName:(NSString *)fileName {
    return [self pathWithType:@"Image" sessionId:sessionId fileName:fileName];
}

+ (NSString *)videofileFilePathWithSessionId:(NSString *)sessionId fileName:(NSString *)fileName {
    return [self pathWithType:@"Video" sessionId:sessionId fileName:fileName];
}

+ (NSString *)audiofileFilePathWithSessionId:(NSString *)sessionId fileName:(NSString *)fileName {
    return [self pathWithType:@"Audio" sessionId:sessionId fileName:fileName];
}

+ (NSString *)fileFilePathWithSessionId:(NSString *)sessionId fileName:(NSString *)fileName {
    return [self pathWithType:@"File" sessionId:sessionId fileName:fileName];
}

+ (NSString *)tempFilePathWithSessionId:(NSString *)sessionId fileName:(NSString *)fileName {
    return [self pathWithType:@"Temp" sessionId:sessionId fileName:fileName];
}


- (NSString *)rootCacheFinder {
    if (!_rootCacheFinder) {
        NSString *finderPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Message"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:finderPath]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:finderPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        _rootCacheFinder = finderPath;
    }
    return _rootCacheFinder;
}

+ (void)deleteFileWithMessageModel:(HJChatMessageModel *)messageModel {
    
    dispatch_async_serial_queue(^{
        NSString *thumbnailPath = [HJMessageFileManager imagefileFilePathWithSessionId:messageModel.sessionId fileName:messageModel.thumbnailPath.lastPathComponent];
        
        NSString *localPath = nil;
        
        if (messageModel.messageType == HJMessageTypeImage) {
            localPath = [HJMessageFileManager imagefileFilePathWithSessionId:messageModel.sessionId fileName:messageModel.localPath.lastPathComponent];
        } else if (messageModel.messageType == HJMessageTypeVoice) {
            localPath = [HJMessageFileManager audiofileFilePathWithSessionId:messageModel.sessionId fileName:messageModel.localPath.lastPathComponent];
        } else if (messageModel.messageType == HJMessageTypeVideo) {
            localPath = [HJMessageFileManager videofileFilePathWithSessionId:messageModel.sessionId fileName:messageModel.localPath.lastPathComponent];
        } else if (messageModel.messageType == HJMessageTypeOther) {
            localPath = [HJMessageFileManager fileFilePathWithSessionId:messageModel.sessionId fileName:messageModel.localPath.lastPathComponent];
        }
        
        if([[NSFileManager defaultManager] fileExistsAtPath:thumbnailPath]){
            [[NSFileManager defaultManager] removeItemAtPath:thumbnailPath error:nil];
        }
        
        if([[NSFileManager defaultManager] fileExistsAtPath:localPath]){
            [[NSFileManager defaultManager] removeItemAtPath:localPath error:nil];
        }
    });
}


+ (void)deleteFileWithSessionId:(NSString *)sessionId {
    dispatch_async_serial_queue(^{
        NSString *sessionPath = [HJMessageFileManager.shareManager.rootCacheFinder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/%@", HJUserInfo.userId, sessionId]];
        if([[NSFileManager defaultManager] fileExistsAtPath:sessionPath]){
            [[NSFileManager defaultManager] removeItemAtPath:sessionPath error:nil];
        }
    });
    
}
@end



